package com.hznu.ez4life.modules.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hznu.ez4life.modules.entity.Assess;
import com.hznu.ez4life.modules.entity.Orders;
import com.hznu.ez4life.modules.dao.OrdersDao;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hznu.ez4life.modules.service.*;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DragonDoor
 * @since 2019-07-25
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> implements OrdersService {

    @Autowired
    BuildingService buildingService;
    @Autowired
    ItemService itemService;
    @Autowired
    UserService userService;
    @Autowired
    AssessService assessService;

    @Override
    public void addOne(Orders orders)
    {
        orders.setCreatetime(new Date(System.currentTimeMillis()));
        this.insert(orders);
    }

    @Override
    public List<Orders> findOrderingByNuserId(Integer nuserId)
    {
        List<Orders> ordersList = this.selectList(new EntityWrapper<Orders>().eq("nuser_id",nuserId).in("statu",new Integer[]{1,2}).orderBy("createtime",false));
        for(Orders orders : ordersList)
        {
            setDetail(orders);
        }
        return ordersList;
    }

    @Override
    public List<Orders> findOrderedByNuserId(Integer nuserId)
    {
        List<Orders> ordersList = this.selectList(new EntityWrapper<Orders>().eq("nuser_id",nuserId).in("statu",new Integer[]{3,4,5}).orderBy("closetime",false));
        for(Orders orders : ordersList)
        {
            setDetail(orders);
            accessFlag(orders);
        }
        return ordersList;
    }

    @Override
    public List<Orders> findOrderingByHuserId(Integer huserId)
    {
        List<Orders> ordersList = this.selectList(new EntityWrapper<Orders>().eq("huser_id",huserId).in("statu",new Integer[]{1,2}).orderBy("createtime",false));
        for(Orders orders : ordersList)
        {
            setDetail(orders);
        }
        return ordersList;
    }

    @Override
    public List<Orders> findOrderedByHuserId(Integer huserId)
    {
        List<Orders> ordersList = this.selectList(new EntityWrapper<Orders>().eq("huser_id",huserId).in("statu",new Integer[]{3,4,5}).orderBy("closetime",false));
        for(Orders orders : ordersList)
        {
            setDetail(orders);
            accessFlag(orders);
        }
        return ordersList;
    }

    @Override
    public Orders findById(Integer id)
    {
        Orders orders = this.selectById(id);
        setDetail(orders);
        return orders;
    }

    private void accessFlag(Orders orders)
    {
        if(orders.getStatu()==3)
        {
            if(assessService.selectObj(new EntityWrapper<Assess>().eq("orders_id",orders.getId()).eq("type",2))!=null)
            {
                orders.setIsAssessed(1);
            }else
            {
                orders.setIsAssessed(0);
            }
        }
    }

    private void setDetail(Orders orders)
    {
        if(orders.getTobuildingId()!=null)
        {
            orders.setTobuilding(buildingService.selectById(orders.getTobuildingId()));
        }
        if(orders.getItemId()!=null)
        {
            orders.setItem(itemService.findById(orders.getItemId()));
        }
        if(orders.getNuserId()!=null)
        {
            orders.setNuser(userService.selectById(orders.getNuserId()));
        }
        if(orders.getHuserId()!=null)
        {
            orders.setHuser(userService.selectById(orders.getHuserId()));
        }
    }
}
